package cn.semcorp.zhuhaizj.listener;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.pojo.dto.CoatingDateDTO;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 涂布产出数据读取分析器
 */
public class ReadCoatingDateListener extends AnalysisEventListener<CoatingDateDTO> {

    /**
     * 读时aop
     * @param coatingDateDTO
     * @param analysisContext
     */
    public List<CoatingDateDTO> coatingDateDTOList ;
    public ReadCoatingDateListener(List<CoatingDateDTO> list){
        this.coatingDateDTOList = list;
    }
    @Override
    public void invoke(CoatingDateDTO coatingDateDTO, AnalysisContext analysisContext) {

        /**
         * 时间格式判断
         */
//        String coatingDate = coatingDateDTO.getDate();
//        LocalDate parse = LocalDate.parse(coatingDate);

        Pattern pattern = Pattern.compile(Common.regex);
        if (!pattern.matcher(coatingDateDTO.getDate()).matches()) {
            throw new ServiceException(ResponseCode.BAD_REQUEST, "日期格式错误,请提供纯数字格式!");
        }

        /**
         * 机台判断
         */
        String machineName = coatingDateDTO.getMachineName();
        if (machineName == null) {
            throw new ServiceException(ResponseCode.BAD_REQUEST,"存在机台名称为空,导入失败!");
        }

        /**
         * 流量、桶装量判断
         */
        String successionTraffic = coatingDateDTO.getSuccessionTraffic(); // 交班流量
        String shiftFlow = coatingDateDTO.getShiftFlow(); // 接班流量
        String successorSlurryWeight = coatingDateDTO.getSuccessorSlurryWeight(); // 交班剩余浆料
        String shiftSlurryWeight = coatingDateDTO.getShiftSlurryWeight(); // 接班剩余浆料
        if ((successionTraffic != null && shiftFlow == null) || (successionTraffic == null && shiftFlow != null)){
            throw new ServiceException(ResponseCode.BAD_REQUEST,"存在循环系统流量单填,导入失败!");
        }
        if ((successorSlurryWeight != null && shiftSlurryWeight == null) || (successorSlurryWeight == null && shiftSlurryWeight != null)){
            throw new ServiceException(ResponseCode.BAD_REQUEST,"存在桶装领料流量单填,导入失败!");
        }

        /**
         * 面密度
         */
        coatingDateDTOList.add(coatingDateDTO);
    }

    /**
     * 读完aop
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 获取数据
     */
    public List<CoatingDateDTO> getCoatingDateDTOList(){
        return coatingDateDTOList;
    }
}
